import config from './config'
import md5 from './md5'

// 接口请求规则
// function getRequestUrl(methods) {
//   let now = Date.parse(new Date())/1000
//   let hash = md5( config.appkey + config.version + methods + config.appid + config.deviceid + config.channel + now )
//   let faid = wx.getStorageSync('FaId') ? wx.getStorageSync('FaId') : 10000;
//   return config.hosts + config.version + '/' + methods + '?appid='+config.appid + '&channel='+ config.channel + '&time=' + now + '&deviceid='+config.deviceid + '&faid='+ faid + '&hash='+hash
// }
function getRequestUrl(url){
  return config.hosts + '/'+url
}

// 获取当前时间戳
function getTimestamp(){
  return Date.parse(new Date())/1000
}

// 时间转时间戳
function formatTimesTamp(t){
  return Date.parse(new Date(`${t}`))||Date.parse(new Date(`${t.replace(/-/g,'/')}`))
}

var EARTH_RADIUS = 6378137.0;    //单位M

var PI = Math.PI;
function getRad(d){
  return d*PI/180.0;
}
// 通过经纬度 获取距离
function getFlatternDistance(lat1,lng1,lat2,lng2){
  var f = getRad((lat1 + lat2)/2);
  var g = getRad((lat1 - lat2)/2);
  var l = getRad((lng1 - lng2)/2);
  var sg = Math.sin(g);
  var sl = Math.sin(l);
  var sf = Math.sin(f);
  var s,c,w,r,d,h1,h2;
  var a = EARTH_RADIUS;
  var fl = 1/298.257;
  sg = sg*sg;
  sl = sl*sl;
  sf = sf*sf;
  s = sg*(1-sl) + (1-sf)*sl;
  c = (1-sg)*(1-sl) + sf*sl;
  w = Math.atan(Math.sqrt(s/c));
  r = Math.sqrt(s*c)/w;
  d = 2*w*a;
  h1 = (3*r -1)/2/c;
  h2 = (3*r +1)/2/s;
  return d*(1 + fl*(h1*sf*(1-sg) - h2*(1-sf)*sg));
}







export default{
  getTimestamp,               // 获取当前时间戳
  getRequestUrl,              // 接口请求规则
  formatTimesTamp,            // 时间转时间戳
  getFlatternDistance,        // 通过经纬度 获取距离
}
